//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.TridentModel
{
    #region Using Directives
    using System;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities;
    using Microsoft.Research.DataLayer;
    using ErActivity = Microsoft.Research.DataLayer.Activity;
    using System.Globalization;
    #endregion

    /// <summary>
    /// This holds the data for the Left pane
    /// </summary>
    [Serializable()]
    public class WorkflowMetadata
    {
        #region Private Member Data
        /// <summary>
        /// ID of the workflow.
        /// </summary>
        private Guid id;

        /// <summary>
        /// This holds Name of the Workflow.
        /// </summary>
        private string name;

        /// <summary>
        /// This holds Description of the Workflow.
        /// </summary>
        private string description;

        /// <summary>
        /// Whether the workflow is deleted or not.
        /// </summary>
        private bool isDeleted;

        #endregion

        #region Constructors
        /// <summary>
        /// Constructor for the workflow model.
        /// </summary>
        /// <param name="rootActivity">E.R. Activity.</param>
        public WorkflowMetadata(ErActivity rootActivity)
        {
            #region Validation of Arguments
            if (null == rootActivity)
            {
                throw new ArgumentNullException("rootActivity");
            }
            #endregion

            if (rootActivity != null && rootActivity.IsWorkflow)
            {
                // Name of the workflow.
                this.name = rootActivity.Name;

                // Description.
                this.description = rootActivity.Description;

                // Id of the workflow.
                this.id = ((IObject)rootActivity).ID;

                // flag for soft delete.
                this.isDeleted = rootActivity.IsDeleted;

                this.VersionLabel = rootActivity.VersionLabel;

                this.LastModifiedTime = rootActivity.Updated.When;

                this.CreatedBy = rootActivity.Created.User;

                this.CreatedTime = rootActivity.Created.When;

                // Owner ID of the workflow.
                this.OwnerId = ((IObject)rootActivity.Owner).ID;
            }
            else
            {
                TridentErrorHandler.ThrowTridentCustomException(
                     TridentErrorConstants.ErrorNumber6000001,
                     TridentResourceManager.GetString("InvalidWorkflowObject"));
            }
        }
        #endregion

        #region Properties

        /// <summary>
        /// Gets the Workflow ID.
        /// </summary>
        public Guid Id
        {
            get
            {
                return this.id;
            }
        }

        /// <summary>
        /// Gets the workflow Name.
        /// </summary>
        public string Name
        {
            get
            {
                return this.name;
            }
        }

        /// <summary>
        /// Gets or sets the version label for the workflow.
        /// </summary>
        public string VersionLabel { get; set; }

        /// <summary>
        /// Gets or sets the workflow Description.
        /// </summary>
        public string Description
        {
            get
            {
                return this.description;
            }

            set
            {
                this.description = value;
            }
        }

        /// <summary>
        /// Gets to know whether the workflow is deleted or not.
        /// </summary>
        public bool IsDeleted
        {
            get
            {
                return this.isDeleted;
            }
        }

        /// <summary>
        /// Gets or sets the Id of the category to which the workflow belongs to.
        /// </summary>
        public Guid ParentCategoryId { get; set; }

        /// <summary>
        /// Gets or sets the Name of the category to which the workflow belongs to.
        /// </summary>
        public string ParentCategoryName { get; set; }

        /// <summary>
        /// Gets the last modified time for the workflow.
        /// </summary>
        public DateTime LastModifiedTime { get; private set; }

        /// <summary>
        /// Gets the created time of the workflow.
        /// </summary>
        public DateTime CreatedTime { get; private set; }

        /// <summary>
        /// Gets the author of the workflow.
        /// </summary>
        public string CreatedBy { get; private set; }

        /// <summary>
        /// Gets or sets the owner id.
        /// </summary>
        /// <value>The owner id.</value>
        public Guid OwnerId { get; private set; }

        /// <summary>
        /// Get the display name for the metadata.
        /// </summary>
        /// <returns></returns>
        public string DisplayName
        {
            get
            {
                return string.IsNullOrEmpty(this.VersionLabel) ?
                    this.Name : string.Format(CultureInfo.InvariantCulture, "{0} ({1})", this.Name, this.VersionLabel);
            }
        }

        #endregion
    }
}
